import json
import tornado.web
from tornado.httpclient import AsyncHTTPClient, HTTPRequest


class HttpHandler(tornado.web.RequestHandler):

    async def get(self):
        """向第三方发送get请求"""
        http = AsyncHTTPClient()
        hr = HTTPRequest("http://127.0.0.1:5000/")
        resp = await http.fetch(hr)
        self.write(resp.body.decode())

    async def post(self):
        """向第三方发送post请求"""
        http = AsyncHTTPClient()
        hr = HTTPRequest(
            url="http://127.0.0.1:5000/",
            method="POST",
            headers={"Content-Type":"application/json"}, # 请求头指定参数类型,json格式提交
            body=json.dumps({"a": "hello", "b":1})  # 请求参数
        )
        resp = await http.fetch(hr)
        self.write(resp.body.decode())